<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first" />
          <el-tab-pane label="公司信息" name="second" />
          <el-row style="height:60px">
            <el-button
              type="primary"
              size="small"
              @click="handleAdd"
            >新增角色</el-button>
          </el-row>
        </el-tabs>
        <el-table
          :data="roleList"
          stripe
          border
          style="width: 100%"
        >
          <el-table-column
            align="center"
            label="序号"
            width="120"
          >
            <template slot-scope="scope">{{ scope.$index+1 }}</template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="name"
            label="角色"
            width="240"
          />
          <el-table-column
            align="center"
            prop="description"
            label="描述"
          />
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                type="success"
                size="small"
                @click="hAssignPer(scope.row.id)"
              >权限分配</el-button>
              <el-button
                type="primary"
                size="small"
                @click="handleEdit(scope.row)"
              >编辑</el-button>
              <el-button
                size="small"
                type="danger"
                @click="handleDelete(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页组件 -->
          <el-pagination
            layout="total,sizes,prev,pager,next"
            :total="total"
            :current-page="p.page"
            :page-sizes="[2, 3, 5, 10]"
            :page-size="p.pagesize"
            @current-change="hchangeCurrent"
            @size-change="handleSizeChange"
          />
        </el-row>
      </el-card>
      <el-dialog
        :title="title"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="hClose">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <el-dialog
        title="分配权限"
        :visible.sync="showDialogAssign"
      >
        <assign-permission
          v-if="showDialogAssign"
          :cur-id="curId"
          @close="showDialogAssign=false"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import assignPermission from './assignPermission'
import { addRole, delrole, getroles, getrolsDetail, updateroleDetail } from '@/api/role'
export default {
  components: {
    assignPermission
  },
  data() {
    return {
      roleList: [],
      total: 10,
      p: {
        page: 1,
        pagesize: 10
      },
      roleForm: {
        name: '',
        description: ''
      },
      activeName: 'first',
      type: 'add',
      showDialog: false,
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }

        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }

        ]
      },
      showDialogAssign: false,
      curId: ''

    }
  },
  computed: {
    title() {
      if (this.type === 'add') {
        return '新增角色'
      } else {
        return '编辑角色'
      }
    }
  },
  async created() {
    this.loadRoles()
  },
  methods: {
    hAssignPer(id) {
      this.curId = id
      this.showDialogAssign = true
    },
    hchangeCurrent(val) {
      this.p.page = val
      this.loadRoles()
    },
    handleSizeChange(val) {
      this.p.pagesize = val
      this.p.page = 1
      this.loadRoles()
    },
    async loadRoles() {
      const res = await getroles(this.p)
      this.total = res.data.total
      this.roleList = res.data.rows
    },
    async  handleEdit(row) {
      this.type = 'edit'
      const res = await getrolsDetail(row.id)
      this.showDialog = true
      // console.log(res)
      this.roleForm = { ...res.data }
    },
    handleAdd() {
      this.tyep = 'add'
      this.showDialog = true
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    handleDelete(id) {
      this.$confirm('确认删除此角色?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delrole(id)
        if (this.roleList.length === 1) {
          this.p.page--
          if (this.p.page <= 0) {
            this.p.page = 1
          }
        }
        this.loadRoles()
        this.$message.success('删除成功!')
      }).catch(() => {
        console.log('cancle')
      })
    },
    async hSubmit() {
      this.$refs.roleForm.validate(async valid => {
        if (!valid) return
        try {
          if (this.type === 'add') {
            await addRole(this.roleForm)
            // console.log(res)
            this.total++
            this.p.page = Math.ceil(this.total / this.p.pagesize)
            this.$message.success('添加成功')
            this.loadRoles()
          } else {
            const res = await updateroleDetail(this.roleForm)
            if (res.code !== 10000) return this.$message.error(res.message)
            this.$message.success(res.message)
            this.$message.success('修改成功')
            this.loadRoles()
          }
        } catch (error) {
          this.$message.error(error)
        }
      })
      this.hClose()
    },
    hClose() {
      this.showDialog = false
      this.roleForm = {
        name: '',
        description: ''
      }
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
